package Extends.CUICompents.CCtrls
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import Extends.CEvent.CustomEvent;
	import Extends.CUICompents.CText.CMText;
	
	public class CMRadioButton extends Sprite
	{
		static public const RADIO_BUTTON_ITEM_CLICK:String="RADIO_BUTTON_ITEM_CLICK";
		private var ms_SelectedState:Bitmap;
		private var ms_backState:Bitmap;
		private var ms_Label:CMText = new CMText();
		private var ms_isSelected:Boolean = false;
		private var ms_value:Object = null;
		public function CMRadioButton(label:String,selected:Boolean=false,backState:DisplayObject=null,selectState:DisplayObject=null)
		{
			super();
			ms_Label.text = label;
			ms_isSelected = selected;
			ms_SelectedState = selectState as Bitmap;
			ms_backState = backState as Bitmap;
			this.selfSuper();
			this.mouseChildren = false;
			this.addEventListener(MouseEvent.CLICK,OnCheckBox);
		}
		private function selfSuper():void
		{
			ms_SelectedState.x = (ms_backState.width-ms_SelectedState.width)/2;
			ms_SelectedState.y = (ms_backState.height-ms_SelectedState.height)/2;
			ms_Label.x = ms_backState.width;
			this.addChild(ms_backState);
			this.addChild(ms_SelectedState);
			this.addChild(ms_Label);
			ms_SelectedState.visible = ms_isSelected;
		}
		private function OnCheckBox(evt:MouseEvent):void
		{
			if(!ms_isSelected)
			{
				this.selected = true;
			}
			var event:CustomEvent = new CustomEvent(CMRadioButton.RADIO_BUTTON_ITEM_CLICK);
			event.selected = this.selected;
			this.dispatchEvent(event);
		}
		public function set selected(bool:Boolean):void
		{
			ms_isSelected = bool;
			ms_SelectedState.visible = bool;
		}
		public function get selected():Boolean
		{
			return ms_isSelected;
		}
		override public function set width(value:Number):void
		{
			ms_Label.width =value- ms_backState.width;
		}
		public function get text():String
		{
			return ms_Label.text;
		}
		public function set value(val:Object):void
		{
			ms_value  = val;
		}
		public function get value():Object
		{
			if(ms_value == null)
			{
				ms_value = ms_Label.text;
			}
			return ms_value;
		}
	}
}